<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('推送日志') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('流程ID')" prop="actId">
        <el-input
          v-model="queryParams.actId"
          :placeholder="$t('流程ID')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('任务ID')" prop="taskId">
        <el-input
          v-model="queryParams.taskId"
          :placeholder="$t('任务ID')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('流程标题')" prop="actTitle">
        <el-input
          v-model="queryParams.actTitle"
          :placeholder="$t('流程标题')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('发送时间')" prop="pushTime">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item :label="$t('发送类型')" prop="pushType">
        <el-select v-model="queryParams.pushType" :placeholder="$t('发送类型')" clearable>
          <el-option
            v-for="dict in pushTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('发送状态')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('发送状态')" clearable>
          <el-option
            v-for="dict in pushStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('用户名称')" prop="actAssignee" width="380">
        <UserSelect
          v-model="queryParams.actAssignee"
          :placeholder="$t('用户名称')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="wfPushLogList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('流程ID')" align="center" prop="actId" width="200" />
      <el-table-column :label="$t('任务ID')" align="center" prop="taskId" width="200" />
      <el-table-column :label="$t('流程标题')" align="center" prop="actTitle" width="200">
        <template slot-scope="scope">
          <el-link type="primary" @click="openTask(scope.row)">{{ scope.row.actTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('发送时间')" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime,'{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('发送类型')" align="center" prop="pushType" :formatter="pushTypeFormat" />
      <el-table-column :label="$t('用户名称')" align="center" prop="userName" />
      <el-table-column :label="$t('发送状态')" align="center" prop="status" :formatter="pushStatusFormat" />
      <el-table-column :label="$t('操作')" align="center" prop="message" show-overflow-tooltip width="200">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="againPush(scope.row)"
          >{{ $t('重新推送') }}</el-button>
          <el-button
            v-if="scope.row.status === 'SUCCESS' && scope.row.pushType === 'TODO'"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="delTodo(scope.row)"
          >{{ $t('删除待办') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :visible.sync="formOpen" :title="$t('任务详情')" :close-on-click-modal="false" custom-class="dialogForm">
      <ProcessInfoForm ref="processInfoForm" />
    </el-dialog>
  </div>
</template>

<script>
  import { listWfPushLog, delTodo, againPush } from '@/api/act/actPushLog/wfPushLog'
  import ProcessInfoForm from '@/views/act/process/processInfoForm'
  export default {
    components: {
      ProcessInfoForm
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 推送日志表格数据
        wfPushLogList: [],
        // 弹出层标题
        title: '推送日志',
        // 是否显示弹出层
        formOpen: false,
        // 发送类型字典
        pushTypeOptions: [],
        // 发送状态
        pushStatusOptions: [],
        // 日期查询范围
        dateRange: [],

        // 查询参数
        queryParams: {
          actId: undefined,
          taskId: undefined,
          pushTime: undefined,
          pushType: undefined,
          actAssignee: undefined,
          pageNum: 1,
          pageSize: 10
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
      this.getDicts('wf_push_type').then(response => {
        this.pushTypeOptions = response.data
      })

      this.getDicts('wf_push_status').then(response => {
        this.pushStatusOptions = response.data
      })
    },
    methods: {
      /** 查询推送日志列表 */
      getList() {
        this.loading = true
        listWfPushLog(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.wfPushLogList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 发送类型字典翻译
      pushTypeFormat(row) {
        return this.selectDictLabel(this.pushTypeOptions, row.pushType)
      },

      // 发送状态字典翻译
      pushStatusFormat(row) {
        return this.selectDictLabel(this.pushStatusOptions, row.status)
      },

      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          actId: undefined,
          taskId: undefined,
          pushTime: undefined,
          pushType: undefined,
          actAssignee: undefined,
          status: undefined,
          message: undefined,
          userName: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined

        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = []
        this.resetForm('queryForm')
        this.queryHandle()
      },

      // 打开流程详情页面
      openTask(row) {
        this.formOpen = true
        this.$nextTick(() => {
          this.$refs.processInfoForm.init(row)
        })
      },
      // 重新推送
      againPush(row) {
        this.$confirm(this.$t('是否确认重新推送待办?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        })
          .then(function() {
            return againPush(row.id)
          })
          .then(() => {
            this.getList()
            this.msgSuccess(this.$t('操作成功'))
          })
          .catch(function() {})
      },
      // 删除待办
      delTodo(row) {
        this.$confirm(this.$t('删除确认删除已推送的待办?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        })
          .then(function() {
            return delTodo(row.id)
          })
          .then(() => {
            this.getList()
            this.msgSuccess(this.$t('操作成功'))
          })
          .catch(function() {})
      }
    }
  }
</script>
